<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="用户名称" prop="userName">
        <el-select
          style="width: 100%"
          v-model="queryParams.userName"
          filterable
          placeholder="请选择用户名称"
          remote
          reserve-keyword
          :remote-method="getUserList"
        >
          <el-option
            v-for="(item, index) in userList"
            :key="index"
            :label="`${item.nickName}(${item.userName})`"
            :value="item.nickName"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择类型"
          clearable
        >
          <el-option label="奖励" value="0" />
          <el-option label="惩罚" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="工序" prop="productType">
        <el-select
          v-model="queryParams.productType"
          placeholder="请选择工序"
          clearable
        >
          <el-option label="窗帘" value="0" />
          <el-option label="窗幔" value="1" />
          <el-option label="其他" value="2" />
          <el-option label="产品原料" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="审核员" prop="auditor">
        <el-input
          v-model="queryParams.auditor"
          placeholder="请输入审核员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核时间" prop="auditTime">
        <el-date-picker
          clearable
          v-model="queryParams.auditTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择审核时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['md:punishment:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['md:punishment:edit']"
          >修改</el-button
        >
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['md:punishment:remove']"
          >删除</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['md:punishment:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="punishmentList"
      @selection-change="handleSelectionChange"
      :max-height="maxHeight"
      border
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="编号"
        align="center"
        prop="awardAndPunishmentId"
      />
      <el-table-column label="用户名称" align="center" prop="userName" />
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="{ row }">
          {{ row.type === "0" ? "奖励" : row.type === "1" ? "惩罚" : "" }}
        </template>
      </el-table-column>
      <el-table-column label="奖惩金额" align="center" prop="amount">
        <template slot-scope="{ row }">
          <span v-if="row.type === '0'" style="color: #67c23a">{{
            row.amount
          }}</span>
          <span v-else-if="row.type === '1'" style="color: red">{{
            row.amount
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="奖惩原因"
        align="center"
        prop="reason"
        min-width="150"
      />
      <el-table-column label="返修单提交问题" align="center" prop="problem" />
      <el-table-column
        label="处理意见"
        align="center"
        prop="handlingSuggestion"
      />
      <el-table-column
        label="加工单号"
        align="center"
        prop="processingOrderNumber"
      />
      <el-table-column label="工序" align="center" prop="productType">
        <template slot-scope="{ row }">
          {{
            row.productType === "0"
              ? "窗帘"
              : row.productType === "1"
              ? "窗幔"
              : row.productType === "2"
              ? "其他"
              : row.productType === "3"
              ? "产品原料"
              : ""
          }}
        </template>
      </el-table-column>
      <el-table-column
        label="发货单号"
        align="center"
        prop="deliveryNoteNumber"
      />
      <el-table-column label="销售单号" align="center" prop="orderNumber" />
      <el-table-column label="客户名 " align="center" prop="clientName" />
      <el-table-column label="模块" align="center" prop="mode" />
      <el-table-column label="序号" align="center" prop="serialNumber" />
      <el-table-column label="订单详情" align="center" prop="orderInfo" />
      <el-table-column label="审核员" align="center" prop="auditor" />
      <el-table-column
        label="审核时间"
        align="center"
        prop="auditTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改奖惩对话框 -->
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      :visible.sync="open"
      width="800px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户名称" prop="userName">
              <el-select
                style="width: 100%"
                v-model="form.userName"
                filterable
                placeholder="请选择用户名称"
                remote
                reserve-keyword
                :remote-method="getUserList"
                @change="handleClientChange"
              >
                <el-option
                  v-for="(item, index) in userList"
                  :key="index"
                  :label="`${item.nickName}(${item.userName})`"
                  :value="item.nickName"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类型" prop="type">
              <el-select
                v-model="form.type"
                placeholder="请选择类型"
                style="width: 100%"
              >
                <el-option label="奖励" value="0" />
                <el-option label="惩罚" value="1" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="奖惩金额" prop="amount">
              <el-input v-model="form.amount" placeholder="请输入奖惩金额" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="奖惩原因" prop="reason">
              <el-input v-model="form.reason" placeholder="请输入奖惩原因" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="返修单提交问题" prop="problem">
              <el-input
                v-model="form.problem"
                placeholder="请输入返修单提交问题"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="处理意见" prop="handlingSuggestion">
              <el-input
                v-model="form.handlingSuggestion"
                placeholder="请输入处理意见"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="加工单号" prop="processingOrderNumber">
              <el-input
                v-model="form.processingOrderNumber"
                placeholder="请输入加工单号"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工序" prop="productType">
              <el-select
                v-model="form.productType"
                placeholder="请选择工序"
                style="width: 100%"
              >
                <el-option label="窗帘" value="0" />
                <el-option label="窗幔" value="1" />
                <el-option label="其他" value="2" />
                <el-option label="产品原料" value="3" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="发货单号" prop="deliveryNoteNumber">
              <el-input
                v-model="form.deliveryNoteNumber"
                placeholder="请输入发货单号"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="销售单号" prop="orderNumber">
              <el-input
                v-model="form.orderNumber"
                placeholder="请输入销售单号"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="客户名 " prop="clientName">
              <el-input v-model="form.clientName" placeholder="请输入客户名 " />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="模块" prop="mode">
              <el-input v-model="form.mode" placeholder="请输入模块" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="序号" prop="serialNumber">
              <el-input v-model="form.serialNumber" placeholder="请输入序号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单详情" prop="orderInfo">
              <el-input v-model="form.orderInfo" placeholder="请输入订单详情" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="审核员" prop="auditor">
              <el-input v-model="form.auditor" placeholder="请输入审核员" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核时间" prop="auditTime">
              <el-date-picker
                clearable
                v-model="form.auditTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择审核时间"
                style="width: 100%"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPunishment,
  getPunishment,
  delPunishment,
  addPunishment,
  updatePunishment,
} from "@/api/money/punishment";
import { listUser } from "@/api/system/user";
import commonMix from "@/utils/mixins/common";

export default {
  mixins: [commonMix],
  name: "Punishment",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 奖惩表格数据
      punishmentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        userId: null,
        userName: null,
        type: null,
        amount: null,
        reason: null,
        problem: null,
        handlingSuggestion: null,
        processingOrderNumber: null,
        productType: null,
        deliveryNoteNumber: null,
        orderNumber: null,
        clientName: null,
        mode: null,
        serialNumber: null,
        orderInfo: null,
        auditor: null,
        auditTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userName: [
          { required: true, message: "请选择用户", trigger: "change" },
        ],
        type: [{ required: true, message: "请选择类型", trigger: "change" }],
      },
      userList: [],
    };
  },
  created() {
    this.getList();
    this.getUserList();
  },
  methods: {
    /** 查询奖惩列表 */
    getList() {
      this.loading = true;
      listPunishment(this.queryParams).then((response) => {
        this.punishmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        awardAndPunishmentId: null,
        userId: null,
        userName: null,
        type: null,
        amount: null,
        reason: null,
        problem: null,
        handlingSuggestion: null,
        processingOrderNumber: null,
        productType: null,
        deliveryNoteNumber: null,
        orderNumber: null,
        clientName: null,
        mode: null,
        serialNumber: null,
        orderInfo: null,
        auditor: null,
        auditTime: null,
        createTime: null,
        createBy: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.awardAndPunishmentId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加奖惩";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const awardAndPunishmentId = row.awardAndPunishmentId || this.ids;
      getPunishment(awardAndPunishmentId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改奖惩";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.awardAndPunishmentId != null) {
            updatePunishment(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPunishment(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const awardAndPunishmentIds = row.awardAndPunishmentId || this.ids;
      this.$modal
        .confirm(
          '是否确认删除奖惩编号为"' + awardAndPunishmentIds + '"的数据项？'
        )
        .then(function () {
          return delPunishment(awardAndPunishmentIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "md/punishment/export",
        {
          ...this.queryParams,
        },
        `punishment_${new Date().getTime()}.xlsx`
      );
    },
    getUserList(nickName = "") {
      listUser({
        pageNum: 1,
        pageSize: 200,
        nickName,
      }).then(({ rows }) => {
        this.userList = rows;
      });
    },
    handleClientChange() {
      const { userId } = this.userList?.find(
        (e) => e.nickName === this.form.userName
      );
      this.form.userId = userId;
    },
  },
};
</script>
